package  
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.net.URLRequest;
	
	/**
	 * ...
	 * @author me
	 */

	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.ui.Keyboard;
	
	
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	
	
	/* Класс кораблика */
	public class ClShip extends ClObject
	{
		// Клавиатура.
		public var krKey:KeyMaster;
		
		/* Перемещение */ 
		public var canFire:Boolean = true;
		public var fireTimer:Timer;
		
		public var firePer:int = 100;
		
		/**/
		public var weaponArrat:Array = new Array();
						
		/* Конструктор */
		public function ClShip(mainRef:Main)
		{
			super(mainRef.stage);
			
			/* Картинка */
			//LoadImage("../content/images/SpaceShip.png");
			if (Main.useEmbededContent)
				LoadEmbededImage(Assets.SpaceShipImage);
			else
				LoadImage("../content/images/SpaceShip.png");
			
			krKey = new KeyMaster(stageRef);
			halfWidth = width/2;
			halfHeight = height/2;
			
			fireTimer = new Timer(firePer, 1);
			fireTimer.addEventListener(TimerEvent.TIMER, FireTimerHandler, false, 0, true);
		}
		
		/* Получение урона */
		public function TakeHit(obj:ClObject):void
		{
			//hp -= obj.hp;
			obj == null;
		}
		
		private function FireTimerHandler(e:TimerEvent):void
		{
			canFire = true;
		}
		
		override protected function InitParams():void
		{
			hp = 10;
			
			x=stageRef.stageWidth/2;
			y=stageRef.stageHeight*3/4;
			xSpeed = 0;
			ySpeed = 0;
			maxXSpeed=15;
			maxYSpeed=15;
			
			speedPower = 2;
			speedFriction = 0.9;
			rotateAngle = 0;
		}
		
		//var sh1:ClMissle;
		/* Выстрел */
		protected function doFire():void
		{
			if (canFire)
			{
				var sh1:ClMissle = new ClMissle(this);
				stageRef.addChild(sh1);		
				sh1.x = this.x-(this.width/2)+15 + this.xSpeed*2;
				sh1.y = this.y - 15
				sh1.speedPower = 30;
				//sh1.xSpeed = xSpeed / 4;
				
				sh1 = null;
				
				sh1 = new ClMissle(this);
				stageRef.addChild(sh1);		
				sh1.x = this.x+(this.width/2)-15 + this.xSpeed*2;
				sh1.y = this.y - 15
				sh1.speedPower = 30;
				//sh1.xSpeed = xSpeed / 4;
				sh1 = null;
				
				canFire = false;
				fireTimer.delay = firePer;
				fireTimer.start();
			}
		}
		
		protected function controlUpdate():void
		{
			if (krKey.isLeft || krKey.isA)
			{
				xSpeed-=speedPower;
			}
			if (krKey.isRight || krKey.isD)
			{
				xSpeed+=speedPower;
			}
			if (krKey.isUp || krKey.isW)
			{
				ySpeed-=speedPower;
			}
			if (krKey.isDown || krKey.isS)
			{
				ySpeed+=speedPower;
			}
			
			/* Выстрел */
			if (krKey.isSpace)
			{
				doFire();
			}
			
			/* Восстановление параметров */
			if (krKey.isBackspace)
			{
				InitParams();
			}
			
			
			/* Ограничение в передвижении */
			if (x<0+halfWidth)
			{
				x=0+halfWidth;
				//xSpeed*=-1/5;
				xSpeed=0;
			}
			if (x>stageRef.stageWidth-halfWidth)
			{
				x=stageRef.stageWidth-halfWidth;
				//xSpeed*=-1/5;
				xSpeed=0;
			}
			if (y>stageRef.stageHeight-height)
			{
				y=stageRef.stageHeight-height;
				//ySpeed*=-1/5;
				ClStar.GlobalSpeed -= 0.1;
			}
			
			if (y<stageRef.stageHeight/2)
			{
				y=stageRef.stageHeight/2;
				ClStar.GlobalSpeed += 0.1;
				//ySpeed*=-1/2
			}
			else
			{
				if (ClStar.GlobalSpeed > 1.0)
					ClStar.GlobalSpeed -= 0.1;
			}
			
			/* Ограничение глобальной скорости */
			if (ClStar.GlobalSpeed < 1.0)
					ClStar.GlobalSpeed = 1.0;
			if (ClStar.GlobalSpeed > 3.0)
					ClStar.GlobalSpeed = 3.0;
			
			
			/* Ограничение скорости */
			if (xSpeed>maxXSpeed)
			{
				xSpeed=maxXSpeed;
			}
			else if (xSpeed<-maxXSpeed)
			{
				xSpeed=-maxXSpeed
			}
			if (ySpeed>maxYSpeed)
			{
				ySpeed=maxYSpeed;
			}
			else if (ySpeed<-maxYSpeed)
			{
				ySpeed = -maxYSpeed;
			}
		}
		
		/* Апдейт */
		override protected function update(e:Event):void
		{
			// Адейт управления.
			controlUpdate();
			
			xSpeed*=speedFriction;
			ySpeed*=speedFriction;
			
			/* Изменение формы при движении */
			this.rotation = xSpeed/4;
			//this.scaleX = (maxXSpeed - Math.abs(xSpeed))/(maxXSpeed*4) + 0.75;
			//this.scaleY = ySpeed;
			
			x+=xSpeed;
			y+=ySpeed;
		}
		
	}
}